﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using QuanLyKhachSanDAO.DTO;
using QuanLyKhachSanDAO.DAO;
namespace QuanLyKhachSanDAO.DAO
{
    public class PhieuThuePhongDAO
    {
        public bool ThemPhieuThue(PhieuThuePhongDTO pt,KhachHangDTO[] kh)
        {
            bool flag = true;
            DataTable tb = new DataTable();
            int tt = 0;
            if (pt.TinhTrang == true)
                tt = 1;
            string sql = string.Format("insert into PhieuThuePhong(MaPhong,NgayBatDau,NgayKetThuc,SoNgayThue,DonGiaThue,TinhTrang) values({0},'{1}','{2}',{3},{4},{5}); select @@identity", pt.MaPhong, pt.NgayBatDau, pt.NgayKetThuc, pt.SoNgayThue, pt.DonGiaThue,tt);
            DataProvider conn = new DataProvider();
            tb = conn.ExecuteQuery(sql);
            int mapt=int.Parse(tb.Rows[0][0].ToString());
            for (int i = 0; i < kh.Length; i++)
            {
                int matk = KhachHangDAO.ThemKhachThue(kh[i]);
                ChiTietPhieuThueDTO ctpt =new ChiTietPhieuThueDTO();
                ctpt.MaPhieuThue=mapt;
                ctpt.MaKhachHang=matk;
                ctpt.TinhTrang = true;
                bool kq = ChiTietPhieuThueDAO.ThemChiTietPhieuThue(ctpt);
                if (kq != true)
                    flag = false;
            }
            return flag;
        }

        public DataTable LayPhieuThue()
        {
            DataTable tb = new DataTable();
            string sql = string.Format("select MaPhieuThuePhong from PhieuThuePhong where MaHoaDon is Null ");
            DataProvider conn = new DataProvider();
            tb = conn.ExecuteQuery(sql);
            return tb;
        }

        public DataTable LayPhieuThueTheoMa(int mapthue)
        {
            DataTable tb = new DataTable();
            string sql = string.Format("select * from PhieuThuePhong where MaPhieuThuePhong=" + mapthue);
            DataProvider conn = new DataProvider();
            tb = conn.ExecuteQuery(sql);
            return tb;
        }

        public  bool UpdatePhieuThue(int mapt, float thanhtien, int mahd)
        {
            bool kq = false;
            string sql = string.Format("update PhieuThuePhong set ThanhTien={0},MaHoaDon={1} where MaPhieuThuePhong={2}", thanhtien, mahd, mapt);
            DataProvider conn = new DataProvider();
            kq = conn.ExecuteNonQuery(sql);
            return kq;
        }
    }
}